﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using System;
using System.IO;

namespace ClockServer.Extensions
{
    public static class ExceptionExtension
    {
 
            public static void UseGlobalExceptionHandler(this WebApplication app)
            {
                app.UseExceptionHandler(appError =>
                {
                    appError.Run(async context =>
                    {
                        context.Response.ContentType = "application/json";

                        var errorFeature = context.Features.Get<IExceptionHandlerFeature>();
                        if (errorFeature != null)
                        {
                            var error_str = errorFeature.Error.ToString();
                            File.WriteAllText($"{DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss")}.error.log", error_str);
                        }
                    });
                });
            }

    }
}
